package cdc.cs.controller;

import cdc.cs.common.JSONResult;
import cdc.cs.entity.SuppliersVO;
import cdc.cs.service.ODataSuppliersService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

/**
 * @ClassName: ODataController
 * @Description: TODO
 * @Author: kevin
 * @Date: 2020/5/8 10:24
 * @Version: 1.0
 */
@Api(value = "Supplier中心", tags = {"Supplier模块相关接口"})
@RestController
@RequestMapping("odata")
public class ODataController {

    @Autowired
    ODataSuppliersService service;

    @PostMapping("/add/one")
    @ApiOperation(value = "添加一个供应商", notes = "添加一个供应商", httpMethod = "POST")
    public JSONResult add(
            @ApiParam(name = "suppliersVO", value = "供应商数据", required = true)
            @RequestBody SuppliersVO suppliersVO) throws IOException {
        service.supplierAddOne(suppliersVO);
        return JSONResult.ok();
    }

    @GetMapping("/total")
    @ApiOperation(value = "统计供应商总数", notes = "统计供应商总数", httpMethod = "GET")
    public JSONResult getAll() throws IOException {
        String count = service.supplierGetAll();
        return JSONResult.ok(count);
    }
    @GetMapping("/getone")
    @ApiOperation(value = "查询一个供应商", notes = "查询一个供应商", httpMethod = "GET")
    public JSONResult getone(
            @ApiParam(name = "id", value = "供应商ID", required = true)
            @RequestParam String id) throws IOException {
        Object result = service.supplierGetOne(id);
        return JSONResult.ok(result);
    }
    @PutMapping("/update")
    @ApiOperation(value = "更新一个供应商", notes = "更新一个供应商", httpMethod = "PUT")
    public JSONResult updateOne(
            @ApiParam(name = "id", value = "供应商ID", required = true)
            @RequestParam String id,
            @ApiParam(name = "suppliersVO", value = "供应商数据", required = true)
            @RequestBody SuppliersVO suppliersVO) throws IOException {
        service.supplierUpdateOne(id, suppliersVO);
        return JSONResult.ok();
    }
    @DeleteMapping("/delete")
    @ApiOperation(value = "删除一个供应商", notes = "删除一个供应商", httpMethod = "DELETE")
    public JSONResult updateOne(
            @ApiParam(name = "id", value = "供应商ID", required = true)
            @RequestParam String id) throws IOException {
        service.supplierDeleteOne(id);
        return JSONResult.ok();
    }
}
